package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.redshoes.R;
import com.smalls.redshoes.ui.activity.VodPlayActivity;
import com.smalls.redshoes.util.DateUtil;
import com.smalls.redshoes.util.ViewUtils;

public class VodPlayControlBottomView extends AutoHideLayout {
    public static final String INIT_NETSPEED_TEXT = "0KB/S";
    public static final String INIT_STREAMING_DURATION_TEXT = "00:00:00";
    public static final double SEEKBAR_MAX_PROGRESS = 10000.0d;
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + "PlayBarBottomLayout":"PlayBarBottomLayout";
    private TextView hasPlayedTimeView;
    private boolean isPrepared;
    private boolean isSeeking;
    private LayoutParams layoutParams;
    private LinearLayout ll_prompt;
    private Context mContext;
    private TextView mMenuHintTv;
    private AccelerationSeekBar mSeekBar;
    private double mTextStep;
    private TextView netSpeedView;
    private OnEventListener onEventListener;
    private TextView streamingName;
    private PaddingTextView text;
    private TextMoveLayout textMoveLayout;
    private TextView totalTimeView;

    public interface OnEventListener {
        void onProgressChanged(int i);

        void onSeekBarSeekedComplete(int i);

        void onSeekLeft();

        void onSeekRight();
    }

    class OnSeekBarKeyListener implements OnKeyListener {
        OnSeekBarKeyListener() {
        }

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == 22 || keyCode == 21) {
                VodPlayControlBottomView.this.delayHide();
                if (event.getAction() == 1) {
                    VodPlayControlBottomView.this.isSeeking = false;
                    int progress = ((SeekBar) v).getProgress();
                    if (VodPlayControlBottomView.this.onEventListener != null) {
                        VodPlayControlBottomView.this.onEventListener.onSeekBarSeekedComplete(progress);
                    }
                } else if (event.getAction() == 0) {
                    VodPlayControlBottomView.this.isSeeking = true;
                    if (keyCode == 22) {
                        if (VodPlayControlBottomView.this.onEventListener != null) {
                            VodPlayControlBottomView.this.onEventListener.onSeekRight();
                        }
                    } else if (keyCode == 21 && VodPlayControlBottomView.this.onEventListener != null) {
                        VodPlayControlBottomView.this.onEventListener.onSeekLeft();
                    }
                }
            }
            return false;
        }
    }

    public VodPlayControlBottomView(Context context) {
        this(context, null);
    }

    public VodPlayControlBottomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VodPlayControlBottomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.isSeeking = false;
        this.isPrepared = false;
        setVisibility(View.VISIBLE);
    }

    protected long onSetDelayHideTime() {
        return VodPlayActivity.PLAY_CONTINUE_COUNT_DOWN_TIME;
    }

    protected void init(Context context) {
        View view;
        setAutoHide(false);
        super.init(context);
        this.mContext = context;

        view = LayoutInflater.from(context).inflate(R.layout.rs_layout_play_bar_bottom, this, true);
        this.ll_prompt = (LinearLayout) view.findViewById(R.id.ll_prompt);
        initNumberSeekBar(context);

        this.streamingName = (TextView) view.findViewById(R.id.layout_playbar_bottom_tv_streaming_name);
        this.hasPlayedTimeView = (TextView) view.findViewById(R.id.layout_playbar_bottom_tv_play_time);
        this.totalTimeView = (TextView) view.findViewById(R.id.layout_playbar_bottom_tv_play_totaltime);
        this.netSpeedView = (TextView) view.findViewById(R.id.layout_playbar_bottom_tv_net_speed);
        this.streamingName = (TextView) view.findViewById(R.id.layout_playbar_bottom_tv_streaming_name);
        this.mSeekBar = (AccelerationSeekBar) view.findViewById(R.id.seekbar);
        this.mSeekBar.setMax(VodPlayActivity.RETRY_TIME_INTERVAL);
        this.mSeekBar.setEnabled(this.isPrepared);
        setListener();
    }

    private void initNumberSeekBar(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((VodPlayActivity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        int screenWidth = metric.widthPixels;
        this.mTextStep = ((double) (screenWidth - ViewUtils.DipToPixels(context, 255.0f))) / SEEKBAR_MAX_PROGRESS;
        this.text = new PaddingTextView(context);
        this.text.setBackgroundResource(R.drawable.rs_play_thumb);
        this.text.setTextColor(-1);
        this.text.setTextSize(22.0f);
        this.layoutParams = new LayoutParams(screenWidth, ViewUtils.DipToPixels(context, 50.0f));
        this.textMoveLayout = (TextMoveLayout) findViewById(R.id.textLayout);
        this.textMoveLayout.addView(this.text, this.layoutParams);
        this.text.layout(0, 0, ViewUtils.DipToPixels(context, 120.0f), ViewUtils.DipToPixels(context, 40.0f));
        this.text.setGravity(1);
        this.text.setText(INIT_STREAMING_DURATION_TEXT);
    }

    private void setListener() {
        this.mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                LogUtil.d(VodPlayControlBottomView.TAG, "onProgressChanged=" + progress);
                if (VodPlayControlBottomView.this.onEventListener != null) {
                    VodPlayControlBottomView.this.onEventListener.onProgressChanged(progress);
                }
                VodPlayControlBottomView.this.text.layout((int) (((double) progress) * VodPlayControlBottomView.this.mTextStep), 0, ((int) (((double) progress) * VodPlayControlBottomView.this.mTextStep)) + ViewUtils.DipToPixels(VodPlayControlBottomView.this.mContext, 120.0f), ViewUtils.DipToPixels(VodPlayControlBottomView.this.mContext, 40.0f));
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        this.mSeekBar.setOnKeyListener(new OnSeekBarKeyListener());
        this.mSeekBar.setFocusable(true);
        this.mSeekBar.requestFocus();
    }

    public void setIsPrepared(boolean isPrepared) {
        this.isPrepared = isPrepared;
        setSeekBarEnabled(this.isPrepared);
    }

    public boolean isSeeking() {
        return this.isSeeking;
    }

    public void setSeekBarEnabled(boolean isEnabled) {
        if (this.mSeekBar != null) {
            this.mSeekBar.setEnabled(isEnabled);
        }
    }

    public void setProgress(int progress) {
        if (progress < 0) {
            progress = 0;
        }
        if (this.mSeekBar != null) {
            this.mSeekBar.setProgress(progress);
            invalidate();
        }
    }

    public SeekBar getSeekBar() {
        return this.mSeekBar;
    }

    public void setStreamingName(String name) {
        if (this.streamingName != null && !TextUtils.isEmpty(name)) {
            this.streamingName.setText(name);
        }
    }

    public void setHasPlayedTime(long second) {
        if (this.hasPlayedTimeView != null) {
            if (second > 0) {
                this.hasPlayedTimeView.setText(DateUtil.showTime(second));
            } else {
                this.hasPlayedTimeView.setText(INIT_STREAMING_DURATION_TEXT);
            }
            if (second > 0) {
                this.text.setText(DateUtil.showTime(second));
            } else {
                this.text.setText(INIT_STREAMING_DURATION_TEXT);
            }
        }
    }

    public void setTotalTime(int second) {
        if (this.totalTimeView == null) {
            return;
        }
        if (second > 0) {
            this.totalTimeView.setText(DateUtil.showTime((long) second));
            if (this.mSeekBar != null) {
                this.mSeekBar.setDuration(second);
                return;
            }
            return;
        }
        this.totalTimeView.setText(INIT_STREAMING_DURATION_TEXT);
    }

    public void setNetSpeed(String netSpeed) {
        if (this.netSpeedView != null && !TextUtils.isEmpty(netSpeed)) {
            this.netSpeedView.setText(netSpeed);
        }
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.onEventListener = onEventListener;
    }

    public View getMenuHintView() {
        return this.ll_prompt;
    }

    public void destory() {
        super.destory();
        clearResource();
    }

    private void clearResource() {
    }

    public void setDuration(int duration) {
        if (this.mSeekBar != null) {
            this.mSeekBar.setDuration(duration);
        }
    }
}
